# See TunecoreDirect#Base
module TunecoreDirect
  
  # Represents an individual TuneCore Album.  Each album belongs to a Person and has one or more Song objects attached to it.
  # To attach artwork to the Album you should place the Artwork file on Amazon S3 and provide the S3 location in the #artwork_s3_url accessor.
  # You can upload the artwork file using !TuneCore's web-based upload tool or you can use a 3rd party S3 interface such as the
  # Amazon_S3_Library_for_REST_in_Ruby[http://developer.amazonwebservices.com/connect/entry.jspa?externalID=135].
  #
  # Stores should be defined using the following abbreviations, seperated by commas:
  # iTunesUS
  # iTunesAU
  # iTunesCA
  # iTunesEU
  # iTunesJP
  # iTunesUK
  # RhapsodyRH
  # MusicNet
  # Napster
  # eMusic
  # Amazon
  # Lala
  # Shockhound
  # amiestreet
  class Album < TunecoreDirect::Base
    include TunecoreDirect
    
    attr_accessor :album_id, :person_id, :name, :orig_release_date, :sale_date, :primary_genre, :secondary_genre
    attr_accessor :artist_name, :label_name, :c_copyright, :p_copyright, :recording_location, :parental_advisory
    attr_accessor :artwork_s3_id, :takedown_at
    attr_accessor :errors 
    attr_accessor :stores, :state, :xml
    attr_accessor :artwork #this should be set to an Artwork object
    
    def initialize(options = {})
      options.each do |k,v|
        self.send("#{k}=", v)
      end
    end
    
    # Creates an Album object from a Rexml:Element
    def self.from_xml_element( album_element)
      @xml = album_element
      album = self.new(:xml => @xml)
      album.album_id = album_element.elements["id"].text
      album.person_id = album_element.elements["person-id"].text 
      album.name = album_element.elements["name"].text
      album.orig_release_date = album_element.elements["orig-release-year"].text
      album.sale_date = album_element.elements["sale-date"].text
      album.primary_genre = album_element.elements["primary-genre"].text 
      album.secondary_genre = album_element.elements["secondary-genre"].text unless album_element.elements["secondary-genre"].nil?
      album.artist_name = album_element.elements["artist-name"].text
      album.label_name = album_element.elements["label-name"].text
      album.c_copyright = album_element.elements["c-copyright"].text
      album.p_copyright = album_element.elements["p-copyright"].text
      album.recording_location = album_element.elements["recording-location"].text
      album.state = album_element.elements["state"].text
      album.takedown_at = album_element.elements["takedown-at"].text
      return album
    end
    
    # Creates a new album
    def create
     req = Request.new
     params = { "person_id" => @person_id, 
                "name" => @name, 
                "orig_release_year" => @orig_release_date, 
                "sale_date" => @sale_date, 
                "primary_genre" => @primary_genre,
                "secondary_genre" => @secondary_genre,
                "artist_name" => @artist_name, 
                "label_name" => @label_name,
                "c_copyright" => @c_copyright, 
                "p_copyright" => @p_copyright,
                "recording_location" => @recording_location,
                "stores" => @stores }
                
      if @artwork.asset_url
        params["artwork_asset_url"] = @artwork.asset_url
      end
      
      res = req.create_album(params)
      raise "Unexpected return type: #{res.type}" unless res.type == "album"
      if res.status == "created"
        @album_id = res.object.album_id
        return true
      else
        @errors = res.errors
        return false
      end
    end
    
    # Returns an Album
    def self.get(album_id)
      req = Request.new
      res = req.get_album(album_id)
      raise "Unexpected return type: #{res.type}" unless res.type == "album"
      return res.object
    end
    
    # Returns all your albums. If a person_id is specified it will return only albums for that person.
    def self.get_all(person_id=nil)
      req = Request.new
      res = req.get_albums(person_id)
      raise "Unexpected return type: #{res.type}" unless res.type == "albums"
      return res.object
    end
        
    # Returns an array that contains every Song in this Album
    def songs
    #  if @xml == nil
    #    self.
      songs = []
      @xml.elements.each("songs/song") do |song_element|
        songs << Song.from_xml_element( song_element)
      end
      return songs
    end
    
    # Returns the number of songs that have been created for this album
    def song_count
      raise "Album#song_count is not implemented in this version of the SDK"
    end
    
    # Once you call Album#finalize the album will be queued for delivery to the stores.  After you call finalize you can no longer make changes to the Album.  
    # All the songs should be created and media assets for the Album should be uploaded to S3.
    def finalize
      raise "Album#finalize is not implemented in this version of the SDK"
    end
    
    # Takes down an album from the stores
    def self.takedown(album_id)
      req = Request.new
      res = req.takedown_album(album_id)
      raise "Unexpected return type: #{res.type}" unless res.type == "album"
      if res.object.takedown_at != nil
        return true
      else
        return false
      end
    end
    
    def is_taken_down?
      !@takedown.nil?
    end
    
    def artwork_upload_url
      "http://#{tunecore_server}/partner/artwork_upload?album_id=#{self.album_id}&api_key=#{api_key}"
    end    
        

  end
end
